const strategys = {
    isNotEmpty: (value, errorMsg) => {
        if(value === ''){
            return errorMsg;
        }
    },
    minLength: (value, length, errorMsg) => {
        if(value.length < length){
            return errorMsg;
        }
    },
    mobileFormat: (value, errorMsg) => {
        if(!/(^1[3|5|8][0-9]{9}$)/.test(value)) {
            return errorMsg;
        }
    }
}



export default class Validator {
    constructor () {
        this.cache = [];
    }

    add (dom, rule, errMsg) {
        let str = rule.split(':');
        console.log(`rule${rule}`)
        console.log(`str1${str}`)
        this.cache.push(() => {
            // str 返回的是 minLength:6 
            console.log(rule)
            console.log(`str2${str}`)
            const strategy = str.shift();
            console.log(strategy)
            str.unshift(dom); // value添加进参数列表
            str.push(errMsg);  // 把errMsg添加进参数列表
            // console.log(dom)
            console.log(`strategy${strategy}`)
            // alert(3)
            return strategys[strategy].apply(dom, str);
        });
    }

    start () {
        for (let i = 0; i < this.cache.length; i++) {
            const validatorFunc = this.cache[i];
            console.log(validatorFunc)
            const msg = validatorFunc()  // 开始效验 并取得效验后的返回信息
            if(msg) {
                return msg
            }
        }
    }
};